<template>
  <div id="app">
    <el-header style="display: flex; justify-content: space-between; align-items: center; background-color: #409EFF; color: white;">
      <span style="font-size: 18px; font-weight: bold;">旅游推荐软件管理系统</span>
      <el-dropdown>
        <span class="el-dropdown-link" style="color: white; cursor: pointer;">
          {{ LoginUser.username }}<i class="el-icon-arrow-down el-icon--right"></i>
        </span>
        <el-dropdown-menu slot="dropdown">
          <el-dropdown-item @click.native="logout">退出登录</el-dropdown-item>
        </el-dropdown-menu>
      </el-dropdown>
    </el-header>
    <el-container>
      <el-aside width="200px">
        <el-menu
          :default-active="$route.path"
          class="el-menu-vertical-demo"
          @select="handleSelect"
          router>
          <el-menu-item index="/">
            <i class="el-icon-menu"></i>
            <span>首页</span>
          </el-menu-item>
          <el-menu-item index="/attractions">
            <i class="el-icon-location"></i>
            <span>景点管理</span>
          </el-menu-item>
          <el-menu-item index="/users">
            <i class="el-icon-user"></i>
            <span>用户管理</span>
          </el-menu-item>
        </el-menu>
      </el-aside>
      <el-main>
        <router-view/>
      </el-main>
    </el-container>
  </div>
</template>

<script>

export default {
  name: 'MainPage',
  data() {
    return {
      LoginUser: null,

    } 
  },
  created() {
    const loginUser = this.$cookies.get('loginUser');
    // console.log('Main页面登录用户信息',loginUser)
    if (loginUser) {
      this.setLoginUser(loginUser);
    } else {
      // 如果没有登录用户信息，跳转到登录页面
      this.$router.push('/');
     }
  },
  methods: {
    setLoginUser(loginUser) {
      this.LoginUser = loginUser;
    },
    handleSelect(key) {
      if (this.$route.path === key) return;
      this.$router.push(key).catch(err => {
        if (err.name === 'NavigationDuplicated') {
          return;
        }
        // console.error('Navigation error:', err);
      })
    },
    
    logout() {
      this.$cookies.remove('loginUser');
      window.location.reload();
    }
  }
}

</script>

<style>
#app {
  font-family: 'Avenir', Helvetica, Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  text-align: center;
  color: #2c3e50;
  margin-top: 60px;
}
</style>